<!DOCTYPE HTML>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="gray-bg">
    
     <div class="container-div">
		<div class="row">
			<div class="col-sm-12 search-collapse">
				<form id="formId">
					<div class="select-list">
						<ul>
						   <li>
								项目：
								<select id="projectId" name="projectId" th:with="type=${projects}" onchange="setSchedulingList()">
								    <option value="">所有项目</option>
	                                <option th:each="projects : ${type}" th:text="${projects.projectName}" th:value="${projects.projectId}" th:selected="${projects.projectId == defaultProjectId}"></option>
	                            </select>
							</li>
							
							<li>
								调度任务：
								<select id="schedulingId" name="schedulingId" th:with="type=${schedulings}">
								    <option value="">所有调度</option>
	                                <option th:each="schedulings : ${type}" th:text="${schedulings.schedulingName}" th:value="${schedulings.schedulingId}" th:selected="${schedulings.schedulingId == defaultSchedulingId}"></option>
	                            </select>
							</li>

							<li>
								任务名称：<input type="text" name="taskName"/>
							</li>

							<li>
								状态：<select name="taskStatus" th:with="type=${@dict.getType('task_execute_status')}">
									<option value="">所有</option>
									<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
								</select>
							</li>

							<li class="select-time">
								<label>时间： </label>
								<input type="text" class="time-input" id="startTime" placeholder="开始时间" name="params[beginTime]" th:value="${startDateStr}"/>
								<span>-</span>
								<input type="text" class="time-input" id="endTime" placeholder="结束时间" name="params[endTime]" th:value="${endDateStr}"/>
							</li>
							
							<li>
								<a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
								<a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
							</li>
						</ul>
					</div>
				</form>
			</div>
			
	        <div class="btn-group-sm hidden-xs" id="toolbar" role="group">
				<shiro:hasPermission name="testexecution:taskExecute:remove">
				<a class="btn btn-danger btn-del btn-del disabled" onclick="$.operate.removeAll()">
					<i class="fa fa-remove"></i> 删除
				</a>
				</shiro:hasPermission>
			</div>
			<div class="col-sm-12 select-table table-striped">
				<table id="bootstrap-table" data-mobile-responsive="true" style="table-layout:fixed"></table>
			</div>
		</div>
	</div>
    <div th:include="include :: footer"></div>
    <script th:inline="javascript">
        var removeFlag = [[${@permission.hasPermi('testexecution:taskExecute:remove')}]];
        var prefix = ctx + "testexecution/taskExecute";
        var schedulingPrefix = ctx + "testexecution/taskScheduling";
        var caseExecutePrefix = ctx + "testexecution/taskCaseExecute";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
				exportUrl: prefix + "/export",
                modalName: "测试任务执行",
				search: false,
				queryParams: function(params) {
	            	return {
	        			// 传递参数查询参数
	                    pageSize:       params.limit,
	                    pageNum:        params.offset / params.limit + 1,
	                    searchValue:    params.search,
	                    orderByColumn:  params.sort,
	                    isAsc:          params.order,
	                    schedulingId:   $('#schedulingId').val(),
	                    projectId:      $('#projectId').val()
	        		};
				},
		        showExport: false,
                columns: [{
		            checkbox: true
		        },
				{
					field : 'taskId', 
					title : '任务ID',
					visible: false
				},
				{
					field : 'schedulingId', 
					title : '调度ID',
					visible: false
				},
				{
					field : 'project.projectName', 
					title : '项目名称',
					width : '9%',
					class : 'myTDLengthHidden'
				},
				{
					field : 'taskName', 
					title : '任务名称',
					width : '25%',
					class : 'myTDLengthHidden',
					formatter : function(value,
							row, index) {
						return '<a href="#" onclick="caseExcuteDetail('+row.taskId+',null'+')">'+ value + '</a> ';
					}
				},
				{
					field : 'createTime', 
					title : '开始时间',
					width : '10%'
				},
				{
					field : 'finishTime', 
					title : '结束时间',
					width : '10%',
					formatter : function(value,
							row, index) {
						if(value==null){
							value = '等待任务结束...';
						}
						return '<font id="finishtime' + row.taskId + '">'+value+'</font>';
					}
				},
				{
					field : 'taskStatus', 
					title : '任务状态',
					width : '15%',
					class : 'myTDLengthHidden',
					formatter : function(value,
							row, index) {
						if(value==1||value==0){
					 	 	var hdiv = '<div class="progress progress-striped active progress-bar-warning" style="margin-bottom:0px">'+
							'<div id="progress'+row.taskId+'" class="progress-bar progress-bar-success" aria-valuemax="100"'+ 
							'aria-valuemin="0" aria-valuenow="0" style="width:0%;text-align:center">0%</div></div>';
							refreshProgress(row.taskId);
						    return  '<font id="progressFont' + row.taskId + '">'+hdiv+'</font>';					
						}else if(value==2){
							return "执行完成";
						}else if(value==3){
							return "任务超时中断";
						}else if(value==4){
							return "唤起客户端失败";
						}else{
							return "异常状态";
						}						
					}
				},
				{
					field : 'caseTotalCount', 
					title : '总共',
					width : '5%'
				},
				{
					field : 'caseSuccCount', 
					title : '成功',
					width : '5%',
					formatter : function(value,
							row, index) {
						return '<a id="casesucc'+row.taskId+'" style="color:#00bf5f" onclick="caseExcuteDetail('+row.taskId+',0'+')">'	+ value + '</a> ';
					}
				},
				{
					field : 'caseFailCount', 
					title : '失败',
					width : '5%',
					formatter : function(value,
							row, index) {
						return '<a id="casefail'+row.taskId+'" style="color:#ff0000" onclick="caseExcuteDetail('+row.taskId+',1'+')">'	+ value + '</a> ';
					}
				},
				{
					field : 'caseLockCount', 
					title : '锁定',
					width : '5%',
					formatter : function(value,
							row, index) {
						return '<a id="caselock'+row.taskId+'" style="color:#FF7F00" onclick="caseExcuteDetail('+row.taskId+',2'+')">'	+ value + '</a> ';
					}
				},
				{
					field : 'caseNoexecCount', 
					title : '执行中',
					width : '6%',
					formatter : function(value,
							row, index) {
						return '<a id="casenoexec'+row.taskId+'" style="color:#6A5ACD" onclick="caseExcuteDetail('+row.taskId+',3'+')">'	+ value + '</a> ';
					}
				}]
            };
            $.table.init(options);
        });
        
        /*设置调度*/
        function setSchedulingList(){
          var projectId = $("#projectId").val();
          if($.common.isEmpty(projectId)) return;
          var url =schedulingPrefix + "/getSchedulingListByProjectId/"+projectId;
          var options = "";
          $.getJSON(url, null, function call(result) {
        	   /*清空原有调度列表*/
        	   while($("#schedulingId").length>1){
        		  $("#schedulingId option[index='1']").remove();
        	    }
        	   /*设置新的调度列表*/
        	    options +=  "<option value=''>所有调度</option>";
               $.each(result, function(i, node){
        		  options +=  "<option value='"+node.schedulingId+"'>"+node.schedulingName+"</option>";
        	    }); 
        	    $("#schedulingId").html(options);
          });
         }

		 /*刷新进度条*/
		 function refreshProgress(taskId){
		   var url = caseExecutePrefix + "/getProgressBarByTaskId/"+taskId;		   
		   $.getJSON(url, null, function call(result) {
				if(null!=document.getElementById("casesucc"+taskId)){
		       		document.getElementById("casesucc"+taskId).innerText = result.caseSuccCount;
		       		document.getElementById("casefail"+taskId).innerText = result.caseFailCount;
		       		document.getElementById("caselock"+taskId).innerText = result.caseLockCount;
		       		document.getElementById("casenoexec"+taskId).innerText = result.caseNoexecCount;
		       		
			       	if(result.taskStatus==0||result.taskStatus==1){
			       		//document.getElementById("casetotal"+taskId).innerText = result.caseTotalCount;
			       		$('#progress'+taskId).css('width', result.taskProgress+'%');
			       		document.getElementById("progress"+taskId).innerText  = result.taskProgress+'%';
			       		$('#progress'+taskId).attr("aria-valuenow",result.taskProgress);
			       	}else if(result.taskStatus==2){
			       		document.getElementById("progressFont"+taskId).innerText = "执行完成";
			       		document.getElementById("finishtime"+taskId).innerText = $.common.formatTime(result.finishTime);
			       		clearTimeout(t);
					}else if(result.taskStatus==3){
			       		document.getElementById("progressFont"+taskId).innerText = "任务超时中断";
			       		document.getElementById("finishtime"+taskId).innerText = $.common.formatTime(result.finishTime);
			       		clearTimeout(t);
					}else if(result.taskStatus==4){
			       		document.getElementById("progressFont"+taskId).innerText = "唤起客户端失败";
			       		document.getElementById("finishtime"+taskId).innerText = $.common.formatTime(result.finishTime);
			       		clearTimeout(t);
					}else{
			       		document.getElementById("progressFont"+taskId).innerText = "异常状态";
			       		document.getElementById("finishtime"+taskId).innerText = $.common.formatTime(result.finishTime);
			       		clearTimeout(t);
					}
				}
		   });
		   var t=setTimeout("refreshProgress("+taskId+")", 2000);
		 }
		 
		 /* 测试用例执行明细 */
		 function caseExcuteDetail(taskId,caseStatus) {
				var url = caseExecutePrefix + '?taskId=' + taskId + '&caseStatus=' + caseStatus;
				createMenuItem(url, "用例明细");
		 }
		 
    </script>
</body>
</html>